using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using WPFMitsuControlsv2;

namespace BookSurfaceDemoApp
{
    /// <summary>
    /// Interaction logic for SurfaceWindow1.xaml
    /// </summary>
    public partial class SurfaceWindow1 : SurfaceWindow
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public SurfaceWindow1()
        {
            InitializeComponent();

            // Add handlers for Application activation events
            AddActivationHandlers();
        }

        #region SURFACE Events

        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled

            //TODO: optionally enable animations here
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
        }

        #endregion

        private void OnLoaded(object sender, RoutedEventArgs args)
        {
        }
        private void NextClick(object sender, RoutedEventArgs args)
        {
            myBook.MoveToNextSheet();
        }
        private void PreviousClick(object sender, RoutedEventArgs args)
        {
            myBook.MoveToPreviousSheet();
        }

        public static string CombineFileInCurrentDirectory(string fileName)
        {
            return System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), fileName);
        }

        private void AutoNextClick(object sender, RoutedEventArgs e)
        {
            myBook.AnimateToIndex(myBook.RightPageIndex + 1, 700, lbFromTop.SelectedIndex == 0);
            myBook.Focus();
        }

        private void AutoPreviousClick(object sender, RoutedEventArgs e)
        {
            myBook.AnimateToIndex(myBook.LeftPageIndex - 1, 700, lbFromTop.SelectedIndex == 0);
            myBook.Focus();
        }

        private void DisplayModeChecked(object sender, RoutedEventArgs e)
        {
            bool result = (sender as CheckBox).IsChecked.Value;
            myBook.DisplayMode = result ? BookDisplayMode.ZoomOnPage : BookDisplayMode.Normal;
        }

        private void AutoPreviousPageClick(object sender, RoutedEventArgs e)
        {
            myBook.MoveToPreviousPage();
        }

        private void AutoNextPageClick(object sender, RoutedEventArgs e)
        {
            myBook.MoveToNextPage();
        }

        private void BookmarksPlacement_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (myBook == null)
                return;

            switch (((SurfaceListBox)sender).SelectedIndex)
            {
                case 0:
                    myBook.BookmarksPlacement = Dock.Left;
                    break;
                case 1:
                    myBook.BookmarksPlacement = Dock.Top;
                    break;
                case 2:
                    myBook.BookmarksPlacement = Dock.Right;
                    break;
                case 3:
                    myBook.BookmarksPlacement = Dock.Bottom;
                    break;

            }
        }

        private void BookmarkerPlacement_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (myBook == null)
                return;

            if (((SurfaceListBox)sender).SelectedIndex == 0)
                myBook.BookmarkerPlacement = Dock.Left;
            else
                myBook.BookmarkerPlacement = Dock.Right;
        }

        private void BookmarksVisibility_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (myBook == null)
                return;

            switch (((SurfaceListBox)sender).SelectedIndex)
            {
                case 0:
                    myBook.BookmarksVisibility = Visibility.Visible;
                    return;
                case 1:
                    myBook.BookmarksVisibility = Visibility.Collapsed;
                    return;
                case 2:
                    myBook.BookmarksVisibility = Visibility.Hidden;
                    return;
            }
        }

        private void myBook_BookmarkAdded(object sender, BookmarkAddedEventArgs e)
        {
            if (e.BookItem is CustomBookItem)
            {
                e.BookItem.Header = new CustomBookmarkProperties() { Color = CustomColors.Green, Details = "Page " + e.PageIndex };

                Dispatcher.BeginInvoke((Action)(() => e.BookItem.BeginEdit()), System.Windows.Threading.DispatcherPriority.Background, null);

                e.IsHeaderHandled = true;
            }
        }
    }
}